

```{r}

################################################################
##### Descriptive Analysis of CE Policy Preferences ######
################################################################

rm(list = ls())

# Load Packages 
library(tidyverse)
#library(devtools)
#devtools::install_github("sirpudika/irpanels")
library(irpanels)
library(haven)


# Load wave data
w8 <- read_dta("S:/data/W8/final_data/upanel_w8.dta") %>% 
  zap_formats() %>% 
  zap_label %>% 
  as.data.frame()

load("S:/data/W8/final_data/sep_w8_research_weighted.RData") 

dataset_weights <- dataset_weights %>%   
  select(pid, sample_weights, w8_weights_raking) %>% 
  rename(PubId = pid)


# Save relevant variables
df_policy <- w8 %>% 
  select(PubId, w8_q34, w8_q36, w8_q9x1,  w8_q9x2, w8_q9x3, w8_q10x1, w8_q10x2, w8_q10x3) %>% 
    filter_all(all_vars(. >= 0))


# Merge df_policy and dataset_weights by the variable PubId
df_policy <- merge(df_policy, dataset_weights, by = "PubId")

# descriptive policy preferences ------------------------------------------


#p1_policy <- plot_bar_v(df_policy, item = w8_q34, question = "On a scale from 1 (completely against) to 5 (completely in favour), \nhow strongly are you in favour or against the mandatory introduction of such a reparability index in Switzerland?") +
#  theme(text = element_text(family = "Arial")) +
#  scale_x_discrete(labels = c("Completely against", "Rather against", "Neither against, nor in favour", "Rather in favour", "Completely in favour"))

#p2_policy <- plot_bar_v(df_policy, item = w8_q36, question = "On a scale from 1 (completely against) to 5 (completely in favour), \nhow strongly are you in favour or against the introduction of a mandatory declaration of the minimum lifespan of products?") +
#  theme(text = element_text(family = "Arial")) +
#  scale_x_discrete(labels = c("Completely against", "Rather against", "Neither against, nor in favour", "Rather in favour", "Completely in favour"))
```

```{r plot 1 with weights}
# agreement to mandatory labelling (with weights) -------------------------

items <- c("On a scale from 1 (completely against) to 5 (completely in favour), how strongly are you in favour or against the mandatory introduction of a repairability index in Switzerland?", 
           "On a scale from 1 (completely against) \nto 5 (completely in favour), how strongly are you in favour or against the \nintroduction of a mandatory declaration of the minimum lifespan of products?")
items <- stringr::str_wrap(items, width = 30)

plot_multiple_h(df_policy, item = c("w8_q34", "w8_q36"), textsize = 12, lang = "EN",
                weights = w8_weights_raking,
                item_labels = items, 
                values = sep_palettes$BluePink,
                labels = c("Completely against                                            ", 
                           "Rather against", "Neither against, nor in favour", "Rather in favour", "Completely in favour")) +
  theme(axis.text.y = element_text(lineheight = 0.8, size = 12), 
        axis.text.x = element_text(lineheight = 0.8, size = 12),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) 

ggsave("agreement_labelling_weights.jpg", width = 9, height = 6)


items <- c("Mandatory introduction of a repairability index", 
           "Introduction of a mandatory declaration of the minimum lifespan of products")
items <- stringr::str_wrap(items, width = 30)

plot_multiple_h(df_policy, item = c("w8_q34", "w8_q36"), textsize = 12, lang = "EN",
                weights = w8_weights_raking,
                item_labels = items, 
                values = sep_palettes$BluePink,
                labels = c("Completely against                                            ", 
                           "Rather against", "Neither against, nor in favour", "Rather in favour", "Completely in favour")) +
  labs(caption = paste('Question text: "On a scale from 1 (completely against) to 5 (completely in favour),\nhow strongly are you in favour or against ... ?"',
                       n_par(df_policy, c("w8_q34", "w8_q36"), lang = "EN"),
                       sep = "\n")) +
  theme(axis.text.y = element_text(lineheight = 0.8, size = 12), 
        axis.text.x = element_text(lineheight = 0.8, size = 12),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) 

ggsave("agreement_labelling_weights_adapted.jpg", width = 9, height = 6)
```

```{r plot 1 without weights}
# same plot without weights ----------------------------------------------
items <- c("On a scale from 1 (completely against) to 5 (completely in favour), how strongly are you in favour or against the mandatory introduction of a repairability index in Switzerland?", 
           "On a scale from 1 (completely against) \nto 5 (completely in favour), how strongly are you in favour or against the \nintroduction of a mandatory declaration of the minimum lifespan of products?")
items <- stringr::str_wrap(items, width = 30)

plot_multiple_h(df_policy, item = c("w8_q34", "w8_q36"), textsize = 12, lang = "EN",
                item_labels = items, 
                values = sep_palettes$BluePink,
                labels = c("Completely against                                            ", 
                           "Rather against", "Neither against, nor in favour", "Rather in favour", "Completely in favour")) +
  theme(axis.text.y = element_text(lineheight = 0.8, size = 12), 
        axis.text.x = element_text(lineheight = 0.8, size = 12),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) 

ggsave("agreement_labelling_NOweights.jpg", width = 9, height = 6)
```

```{r plot 2 with weights}
# # agreement to impact of CE on Swiss economy (with weights) -------------

# w8_q10x1 <- factor(w8$w8_q10x1, levels = c(1, 2, 3, 4, 5), 
#                    labels = c("Verringert das die Wettbewerbsfähigkeit der Schweiz",
#                               "2",
#                               "Macht das keinen Unterschied für die Wettbewerbsfähigkeit der Schweiz",
#                               "4",
#                               "Erhöht das die Wettbewerbsfähigkeit der Schweiz"))
# 
# # Display the original factor variable
# print(w8_q10x1)
# 
# # Define the new labels
# new_labels <- c("1", "2", "3", "4", "5")
# 
# # Change the labels of the factor variable
# levels(w8_q10x1) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q10x1)
# 
# df_policy$w8_q10x1 <- as.character(df_policy$w8_q10x1)
# 
# # Your original factor variable
# w8_q10x2 <- factor(c(1, 2, 3, 4, 5), labels = c("Gehen unter dem Strich Arbeitsplätze verloren",
#                                                 "2",
#                                                 "Macht das keinen Unterschied für die Zahl der Arbeitsplätze",
#                                                 "4",
#                                                 "Werden unter dem Strich Arbeitsplätze geschaffen"))
# 
# # Display the original factor variable
# print(w8_q10x2)
# 
# 
# # Change the labels of the factor variable
# levels(w8_q10x2) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q10x2)
# 
# df_policy$w8_q10x2 <- as.character(df_policy$w8_q10x2)
# 
# # Your original factor variable
# w8_q10x3 <- factor(c(1, 2, 3, 4, 5), labels = c("Verringert das die Wettbewerbsfähigkeit der Schweiz",
#                                                 "2",
#                                                 "Macht das keinen Unterschied für die Wettbewerbsfähigkeit der Schweiz",
#                                                 "4",
#                                                 "Erhöht das die Wettbewerbsfähigkeit der Schweiz"))
# 
# # Display the original factor variable
# print(w8_q10x3)
# 
# 
# # Change the labels of the factor variable
# levels(w8_q10x3) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q10x3)
# 
# df_policy$w8_q10x3 <- as.character(df_policy$w8_q10x3)

item_labels <- c("...it will benefit the Swiss economy (5) - it will make no difference to the Swiss economy (3) - it will harm the Swiss economy (1)", 
           "...all in all, jobs will be created (5) - it will make no difference to the number of jobs (3) - all in all, jobs will be lost (1)",
           "...it will increase Switzerland’s competitiveness (5) - it will make no difference to Switzerland’s competitiveness (3) - it will reduce Switzerland’s competitiveness (1)")
item_labels <- stringr::str_wrap(item_labels, width = 30)
q <- "To save resources,\nthere are sometimes calls to use more recycled materials in the manufacture of products. \nThink for example of packaging or building materials. In the following, we are interested \nin your assessment of how this affects different aspects of the economy."

# df_policy <- df_policy %>% 
#   mutate(w8_q10x1 = as.numeric(w8_q10x1),
#          w8_q10x2 = as.numeric(w8_q10x2),
#          w8_q10x3 = as.numeric(w8_q10x3))

plot_multiple_h(df_policy, item = c("w8_q10x1", "w8_q10x2", "w8_q10x3"), 
                                barwidth = 0.5, lang = "EN", textsize = 10,
                                weights = w8_weights_raking,
                                item_labels = item_labels, question = q,
                                values = sep_palettes$BluePink) +
  ggtitle("If Swiss companies use more recycled materials\nto manufacture products, ...") + 
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial"))

ggsave("agreement_recycling_impact_weights.jpg", height = 5)

item_labels <- c("... it will harm the Swiss economy (1) - it will make no difference to the Swiss economy (3) - it will benefit the Swiss economy (5)", 
           "... all in all, jobs will be lost (1) - it will make no difference to the number of jobs (3) - all in all, jobs will be created (5)",
           "... it will reduce Switzerland’s competitiveness (1) - it will make no difference to Switzerland’s competitiveness (3) - it will increase Switzerland’s competitiveness (5)")
item_labels <- item_labels %>% 
  str_replace_all(" - ", " -\n") %>% 
  str_replace_all("difference .* \\(3\\)", "difference \\(3\\)") %>% 
  str_replace_all("Switzerland", "\nSwitzerland")
q <- "To save resources,\nthere are sometimes calls to use more recycled materials in the manufacture of products. \nThink for example of packaging or building materials. In the following, we are interested \nin your assessment of how this affects different aspects of the economy."

plot_multiple_h(df_policy, item = c("w8_q10x1", "w8_q10x2", "w8_q10x3"), 
                                barwidth = 0.5, lang = "EN", textsize = 10,
                                weights = w8_weights_raking,
                                item_labels = item_labels, question = q,
                                values = sep_palettes$BluePink) + 
  ggtitle("If Swiss companies use more recycled materials\nto manufacture products, ...") +
  scale_y_reverse(labels = paste0(c(100, 75, 50, 25, 0), "%")) +
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) +
  guides(fill = guide_legend(keywidth = 0.4, keyheight = 0.8, 
                             label = T, reverse = F, title = NULL))

ggsave("agreement_recycling_impact_weights_adapted.jpg", height = 5)
```

```{r plot 2 without weights}
# same plot without weights ----------------------------------------------
item_labels <- c("...it will benefit the Swiss economy (5) - it will make no difference to the Swiss economy (3) - it will harm the Swiss economy (1)", 
           "...all in all, jobs will be created (5) - it will make no difference to the number of jobs (3) - all in all, jobs will be lost (1)",
           "...it will increase Switzerland’s competitiveness (5) - it will make no difference to Switzerland’s competitiveness (3) - it will reduce Switzerland’s competitiveness (1)")
item_labels <- stringr::str_wrap(item_labels, width = 30)
q <- "To save resources,\nthere are sometimes calls to use more recycled materials in the manufacture of products. \nThink for example of packaging or building materials. In the following, we are interested \nin your assessment of how this affects different aspects of the economy."

plot_multiple_h(df_policy, item = c("w8_q10x1", "w8_q10x2", "w8_q10x3"), 
                                barwidth = 0.5, lang = "EN", textsize = 10,
                                item_labels = item_labels, question = q,
                                values = sep_palettes$BluePink) +
  ggtitle("If Swiss companies use more recycled materials\nto manufacture products, ...") + 
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial"))

ggsave("agreement_recycling_impact_NOweights.jpg", height = 5)
```

```{r plot 3 with weights}
# agreement to impact of CE on Swiss economy

# w8_q9x1 <- factor(c(1, 2, 3, 4, 5), labels = c("Verringert das die Wettbewerbsfähigkeit der Schweiz",
#                                                "2",
#                                                "Macht das keinen Unterschied für die Wettbewerbsfähigkeit der Schweiz",
#                                                "4",
#                                                "Erhöht das die Wettbewerbsfähigkeit der Schweiz"))
# 
# # Display the original factor variable
# print(w8_q9x1)
# 
# # Define the new labels
# new_labels <- c("1", "2", "3", "4", "5")
# 
# # Change the labels of the factor variable
# levels(w8_q9x1) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q9x1)
# 
# df_policy$w8_q9x1 <- as.character(df_policy$w8_q9x1)
# 
# # Your original factor variable
# w8_q9x2 <- factor(c(1, 2, 3, 4, 5), labels = c("Gehen unter dem Strich Arbeitsplätze verloren",
#                                                "2",
#                                                "Macht das keinen Unterschied für die Zahl der Arbeitsplätze",
#                                                "4",
#                                                "Werden unter dem Strich Arbeitsplätze geschaffen"))
# 
# # Display the original factor variable
# print(w8_q9x2)
# 
# 
# # Change the labels of the factor variable
# levels(w8_q9x2) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q9x2)
# 
# df_policy$w8_q9x2 <- as.character(df_policy$w8_q9x2)
# 
# # Your original factor variable
# w8_q9x3 <- factor(c(1, 2, 3, 4, 5), labels = c("Verringert das die Wettbewerbsfähigkeit der Schweiz",
#                                                "2",
#                                                "Macht das keinen Unterschied für die Wettbewerbsfähigkeit der Schweiz",
#                                                "4",
#                                                "Erhöht das die Wettbewerbsfähigkeit der Schweiz"))
# 
# # Display the original factor variable
# print(w8_q9x3)
# 
# 
# # Change the labels of the factor variable
# levels(w8_q9x3) <- new_labels
# 
# # Display the factor variable with updated labels
# print(w8_q9x3)
# 
# df_policy$w8_q9x3 <- as.character(df_policy$w8_q9x3)

item_labels <- c("...it will benefit the Swiss economy (5) - it will make no difference to the Swiss economy (3) - it will harm the Swiss economy (1)", 
                 "...all in all, jobs will be created (5) - it will make no difference to the number of jobs (3) - all in all, jobs will be lost (1)",
                 "...it will increase Switzerland’s competitiveness (5) - it will make no difference to Switzerland’s competitiveness (3) - it will reduce Switzerland’s competitiveness (1)")
item_labels <- stringr::str_wrap(item_labels, width = 30)
q <- "To save resources, it is sometimes demanded that products are manufactured to be durable,\ni.e., to function for as long as possible and not to break down. \nThink, for example, of a washing machine that is functional for double the time than it usually is nowadays. \nIn the following, we are interested in your assessment of\nhow improved product lifetimes affect various aspects of the economy."

# 
# df_policy <- df_policy %>%
#   mutate(w8_q9x1 = as.numeric(w8_q9x1),
#          w8_q9x2 = as.numeric(w8_q9x2),
#          w8_q9x3 = as.numeric(w8_q9x3))

plot_multiple_h(df_policy, item = c("w8_q9x1", "w8_q9x2", "w8_q9x3"), 
                barwidth = 0.5, lang = "EN", textsize = 10,
                weights = w8_weights_raking,
                item_labels = item_labels, question = q,
                values = sep_palettes$BluePink) +
  ggtitle("If Swiss companies produce more durable products, ...") + 
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial"))
ggsave("agreement_durable_impact_weights.jpg", height = 5)


item_labels <- c("... it will harm the Swiss economy (1) - it will make no difference to the Swiss economy (3) - it will benefit the Swiss economy (5)", 
                 "... all in all, jobs will be lost (1) - it will make no difference to the number of jobs (3) - all in all, jobs will be created (5)",
                 "... it will reduce Switzerland’s competitiveness (1) - it will make no difference to Switzerland’s competitiveness (3) - it will increase Switzerland’s competitiveness (5)")
item_labels <- item_labels %>% 
  str_replace_all(" - ", " -\n") %>% 
  str_replace_all("difference .* \\(3\\)", "difference \\(3\\)") %>% 
  str_replace_all("Switzerland", "\nSwitzerland")
  q <- "To save resources, it is sometimes demanded that products are manufactured to be durable,\ni.e., to function for as long as possible and not to break down. \nThink, for example, of a washing machine that is functional for double the time than it usually is nowadays. \nIn the following, we are interested in your assessment of\nhow improved product lifetimes affect various aspects of the economy."

# 
# df_policy <- df_policy %>%
#   mutate(w8_q9x1 = as.numeric(w8_q9x1),
#          w8_q9x2 = as.numeric(w8_q9x2),
#          w8_q9x3 = as.numeric(w8_q9x3))

plot_multiple_h(df_policy, item = c("w8_q9x1", "w8_q9x2", "w8_q9x3"), 
                barwidth = 0.5, lang = "EN", textsize = 10,
                weights = w8_weights_raking,
                item_labels = item_labels, question = q,
                values = sep_palettes$BluePink) + 
  ggtitle("If Swiss companies produce more durable products, ...") + 
  scale_y_reverse(labels = paste0(c(100, 75, 50, 25, 0), "%")) +
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) +
  guides(fill = guide_legend(keywidth = 0.4, keyheight = 0.8, 
                             label = T, reverse = F, title = NULL))
ggsave("agreement_durable_impact_weights_adapted.jpg", height = 5)
```

```{r plot 3 without weights}
# same plot without weights ----------------------------------------------
item_labels <- c("...it will benefit the Swiss economy (5) - it will make no difference to the Swiss economy (3) - it will harm the Swiss economy (1)", 
                 "...all in all, jobs will be created (5) - it will make no difference to the number of jobs (3) - all in all, jobs will be lost (1)",
                 "...it will increase Switzerland’s competitiveness (5) - it will make no difference to Switzerland’s competitiveness (3) - it will reduce Switzerland’s competitiveness (1)")
item_labels <- stringr::str_wrap(item_labels, width = 30)
q <- "To save resources, it is sometimes demanded that products are manufactured to be durable,\ni.e., to function for as long as possible and not to break down. \nThink, for example, of a washing machine that is functional for double the time than it usually is nowadays. \nIn the following, we are interested in your assessment of\nhow improved product lifetimes affect various aspects of the economy."

plot_multiple_h(df_policy, item = c("w8_q9x1", "w8_q9x2", "w8_q9x3"), 
                barwidth = 0.5, lang = "EN", textsize = 10,
                item_labels = item_labels, question = q,
                values = sep_palettes$BluePink) +
  ggtitle("If Swiss companies produce more durable products, ...") + 
  theme(axis.text.y = element_text(lineheight = 0.8, size = 10),
        axis.text.x = element_text(lineheight = 0.8, size = 10),
        plot.caption = element_text(size = 10),
        legend.text = element_text(size = 10),
        text = element_text(size = 10, family = "Arial")) 
ggsave("agreement_durable_impact_NOweights.jpg", height = 5)
```
